﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyDoiXe
{
    public partial class frmTimKiemXe : Form
    {
        public frmTimKiemXe()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmTimKiemXe_Load(object sender, EventArgs e)
        {
            btnDongY.Enabled = false;
            dtgKetQua.Enabled = false;
            cboHangXe.Enabled = false;
            cboLoaiHang.Enabled = false;
            cboTrongTai.Enabled = false;

            cboHangXe.DataSource = HangXeBUS.LayDanhSach();
            cboHangXe.DisplayMember = "TenHangXe";
            cboHangXe.SelectedIndex = -1;

            cboLoaiHang.DataSource = LoaiHangBUS.LayDanhSach();
            cboLoaiHang.DisplayMember = "TenLoaiHang";
            cboLoaiHang.SelectedIndex = -1;

            cboTrongTai.DataSource = TrongTaiBUS.LayDanhSach();
            cboTrongTai.DisplayMember = "GiaTriTrongTai";
            cboTrongTai.SelectedIndex = -1;

            lblDonVi.Text = TrongTaiBUS.LayDonVi();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            dtgKetQua.Enabled = true;

            HangXeDTO hangXeDto = new HangXeDTO();
            LoaiHangDTO loaiHangDto = new LoaiHangDTO();
            TrongTaiDTO trongtaiDto = new TrongTaiDTO();

            List<XeDTO> lstXe = null;

            if (cboHangXe.SelectedIndex != -1)
            {
                hangXeDto = (HangXeDTO)cboHangXe.SelectedItem;
            }
            if (cboLoaiHang.SelectedIndex != -1)
            {
                loaiHangDto = (LoaiHangDTO)cboLoaiHang.SelectedItem;
            }
            if (cboTrongTai.SelectedIndex != -1)
            {
                trongtaiDto = (TrongTaiDTO)cboTrongTai.SelectedItem;
            }

            if ((cboHangXe.SelectedIndex != -1) && (cboLoaiHang.SelectedIndex != -1) && (cboTrongTai.SelectedIndex != -1))
            {
                lstXe = XeBUS.LayDanhSach(loaiHangDto.MaLoaiHang, hangXeDto.MaHangxe, trongtaiDto.MaTrongTai);
            }
            else if ((cboHangXe.SelectedIndex != -1) && (cboLoaiHang.SelectedIndex != -1))
            {
                lstXe = XeBUS.LayDanhSach(loaiHangDto.MaLoaiHang, hangXeDto.MaHangxe, LoaiTimKiemHaiThamSo.LHHX);
            }
            else if ((cboLoaiHang.SelectedIndex != -1) && (cboTrongTai.SelectedIndex != -1))
            {
                lstXe = XeBUS.LayDanhSach(loaiHangDto.MaLoaiHang, trongtaiDto.MaTrongTai, LoaiTimKiemHaiThamSo.LHTT);
            }
            else if ((cboHangXe.SelectedIndex != -1) && (cboTrongTai.SelectedIndex != -1))
            {
                lstXe = XeBUS.LayDanhSach(hangXeDto.MaHangxe, trongtaiDto.MaTrongTai, LoaiTimKiemHaiThamSo.HXTT);
            }
            else if (cboHangXe.SelectedIndex != -1)
            {
                lstXe = XeBUS.LayDanhSach(hangXeDto.MaHangxe, LoaiTimKiemMotThamSo.HX);
            }
            else if (cboLoaiHang.SelectedIndex != -1)
            {
                lstXe = XeBUS.LayDanhSach(loaiHangDto.MaLoaiHang, LoaiTimKiemMotThamSo.LH);
            }
            else if (cboTrongTai.SelectedIndex != -1)
            {
                lstXe = XeBUS.LayDanhSach(trongtaiDto.MaTrongTai, LoaiTimKiemMotThamSo.TT);
            }

            dtgKetQua.DataSource = lstXe;
        }

        private void chkHangXe_CheckedChanged(object sender, EventArgs e)
        {
            if (chkHangXe.Checked == true)
            {
                cboHangXe.Enabled = true;
            }
            else
            {
                cboHangXe.Enabled = false;
                cboHangXe.SelectedIndex = -1;
            }
        }

        private void chkLoaiHang_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLoaiHang.Checked == true)
            {
                cboLoaiHang.Enabled = true;
            }
            else
            {
                cboLoaiHang.Enabled = false;
                cboLoaiHang.SelectedIndex = -1;
            }
        }

        private void chkTrongTai_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTrongTai.Checked == true)
            {
                cboTrongTai.Enabled = true;
            }
            else
            {
                cboTrongTai.Enabled = false;
                cboTrongTai.SelectedIndex = -1;
            }
        }

        private void cboHangXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboHangXe.SelectedIndex != -1)
            {
                btnDongY.Enabled = true;
            }
            else if( cboLoaiHang.SelectedIndex == -1 && cboTrongTai.SelectedIndex == -1)
            {
                btnDongY.Enabled = false;
                dtgKetQua.Enabled = false;
            }
        }

        private void cboLoaiHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoaiHang.SelectedIndex != -1)
            {
                btnDongY.Enabled = true;
            }
            else if (cboHangXe.SelectedIndex == -1 && cboTrongTai.SelectedIndex == -1)
            {
                btnDongY.Enabled = false;
                dtgKetQua.Enabled = false;
            }
        }

        private void cboTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboTrongTai.SelectedIndex != -1)
            {
                btnDongY.Enabled = true;
            }
            else if (cboHangXe.SelectedIndex == -1 && cboLoaiHang.SelectedIndex == -1)
            {
                btnDongY.Enabled = false;
                dtgKetQua.Enabled = false;
            }
        }
    }
}
